package com.tianqu.tms.www.financia.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TmsFinanciaDto;
import com.tianqu.tms.dto.TmsFinanciaParam;
import com.tianqu.tms.entity.TmsFinancial;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.financia.controller.FinanciaCompanyCreationResultController;
import com.tianqu.tms.www.financia.viewhelper.FinanciaCompanyInputViewHelper;

public class FinanciaCompanyCreationResultAction  extends TmsAction {

    private Logger logger = Logger.getLogger(FinanciaCompanyCreationResultAction.class);
	
	private static final String SYSPATH = "sysPath";
	
	private static final String LEFTLINE ="/";
	
	private static final String TMSPATH = "/tmsweb";
	private static final String BACKFLAG = "backFlag";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		//isToken(mapping, request);
		FinanciaCompanyInputViewHelper viewhelp = (FinanciaCompanyInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		request.setAttribute(TmsConstants.VIEWHELP, viewhelp);
		
		TmsFinancial financial = TmsFinancial.getInstance();
		if(!StringUtil.isNotBlank(viewhelp.getId())) {//add
		    //判断商户名称是否重复
			boolean result1 = financial.isRepeatName(viewhelp.getFinanciaName(),1);
			if(result1){
				setErrorsToRequest(request, TmsConstants.U4001 ,null);
				return mapping.getInputForward();
			}
			
			//判断商户短名是否重复
			boolean result2 = financial.isRepeatName(viewhelp.getShortName(),2);
			if(result2){
				setErrorsToRequest(request, TmsConstants.U4002 ,null);
				return mapping.getInputForward();
			}
			
			//判断用户是否重复
			User user = User.getInstance();
			boolean result = user.isRepeatUserId(viewhelp.getUserId());
			if(result){
				setErrorsToRequest(request, TmsConstants.U4003 ,null);
				return mapping.getInputForward();
			}
		}else {
			TmsFinanciaDto dto = financial.getTmsFinancia(viewhelp.getId());
			if(!viewhelp.getFinanciaName().equals(dto.getFinanciaName())) {
				//判断商户名称是否重复
				boolean result1 = financial.isRepeatName(viewhelp.getFinanciaName(),1);
				if(result1){
					setErrorsToRequest(request, TmsConstants.U4001 ,null);
					return mapping.getInputForward();
				}
			}
			
			if(!viewhelp.getShortName().equals(dto.getShortName())) {
				//判断商户短名是否重复
				boolean result2 = financial.isRepeatName(viewhelp.getShortName(),2);
				if(result2){
					setErrorsToRequest(request, TmsConstants.U4002 ,null);
					return mapping.getInputForward();
				}
			}
			
			if(!viewhelp.getUserId().equals(dto.getUserId())) {
				//判断用户是否重复
				User user = User.getInstance();
				boolean result = user.isRepeatUserId(viewhelp.getUserId());
				if(result){
					setErrorsToRequest(request, TmsConstants.U4003 ,null);
					return mapping.getInputForward();
				}
			}
		}
		
		FinanciaCompanyCreationResultController controller = new FinanciaCompanyCreationResultController();
		uc.setAttribute(SYSPATH, request.getSession().getServletContext().getRealPath(LEFTLINE).replace(TMSPATH, TmsConstants.BLANK));
		controller.execute(uc);
		
		viewhelp.setBackFlag("1");
		request.setAttribute(TmsConstants.VIEWHELP, viewhelp);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS); 
	}
}
